package e.g.com.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.jws.WebParam.Mode;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import e.g.com.pojo.SubBianJi;
import e.g.com.pojo.Tb_subject;
import e.g.com.pojo.Tb_user;
import e.g.com.service.SubjectService;
import e.g.com.service.UserService;

@Controller
public class SubConteoller {
	@Resource
	private SubjectService subs;
	private Tb_subject sub;
	@RequestMapping(value="/manage/syuelan",method=RequestMethod.GET)
	public String findPage(@RequestParam("pagenow")int pagenow,Model m){
		List<Tb_subject>list=subs.findpage(pagenow);
		int cout=subs.findall().size();
		int size;
		
		if(cout%10==0)size=cout/10-1;
		else if(cout<10)size=1;
		else  size=cout/10;
		m.addAttribute("page",pagenow );
		m.addAttribute("sub",list );
		m.addAttribute("size",size );
		System.out.println(list.size());
		return"/manage/subyuelan";
	}
	@RequestMapping(value="/manage/subadd",method=RequestMethod.POST)
	public String addsub(SubBianJi sub,Model m){
		Tb_subject u=new Tb_subject();
		u.setDescription(sub.getDescription());
		u.setId(sub.getId());
		u.setName(sub.getName());
		subs.insert(u);
		return findPage(0, m);
	}
	@RequestMapping(value="/manage/sbianji",method=RequestMethod.GET)
	public String update(@RequestParam("id")int id,Model m){
		Tb_subject tbu=subs.findById(id);
		m.addAttribute("id",tbu.getId());
		m.addAttribute("name",tbu.getName());
		m.addAttribute("description",tbu.getDescription());
		return "/manage/subbianji";
	}
	@RequestMapping(value="/manage/subbianjiin",method=RequestMethod.POST)
	public String updatein(SubBianJi s,Model m){
		Tb_subject sub=new Tb_subject();
		sub.setId(s.getId());
		sub.setName(s.getName());
		sub.setDescription(s.getDescription());
		subs.update(sub);
		int pagenow=0;
		return findPage(pagenow, m);
	}
}
